package com.jackchen.common.i18n;

/**
 * 数据库模型国际化组件
 * 数据库里面需要国际化的字段，在映射文件里面配置该类
 */
public class ModelString{

	// 当前语言编码
	public String currentLangCode;
	// 默认语言编码
	public String defaultLangCode = "cn";
	// 保存XML数据库字段内容
	private String xmlContent = "";
	// 缓存当前编码下的字段值
	private String value;

	public String getXmlContent() {
		return xmlContent;
	}

	public void setXmlContent(String xmlContent) {
		if (xmlContent == null) {
			this.xmlContent = "<items></items>";
			value = null;
		} else {
			if (xmlContent.startsWith("<items>")) {
				// XML解析
				XmlLanguageParser parser = new XmlLanguageParser(xmlContent);
				// 根据当前编码，解析出字段内容
				value = parser.getString(currentLangCode);
				this.xmlContent = xmlContent;
			} else {
				value = xmlContent;
				this.xmlContent = "<items></items>";
			}
		}
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		XmlLanguageParser parser = new XmlLanguageParser(xmlContent);
		parser.setString(currentLangCode, value);
		this.xmlContent = parser.getXmlContent();
		this.value = value;
	}

	public ModelString() {
		String lang = UserLocaleCache.getInstance().getLangFlag();
		if (lang == null) {
			lang = "cn";
		}
		if (lang != null) {
			this.currentLangCode = (String) lang;
		} else
			this.currentLangCode = this.defaultLangCode;
	}

	public String toString() {
		if (value != null) {
			return value;
		}
		return "Please use the value property.";
	}
}
